package wkt.notice;

import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import net.xnzn.framework.data.mybatis.Paging;
import net.xnzn.framework.id.Id;
import net.xnzn.framework.secure.filter.annotation.RequiresAuthentication;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/notice")
@Validated
public class NoticeController {
    @Autowired
    private NoticeMapper noticeMapper;

    @PostMapping("/list/{pageSize}/{pageNum}")
    public Paging<Notice> list(@RequestBody Notice notice, @PathVariable @Range(min = 1, max = 100, message = "请输入1-100") int pageSize, @PathVariable int pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        final LambdaQueryWrapper<Notice> queryWrapper = Wrappers.lambdaQuery(Notice.class)
                .select(Notice::getId, Notice::getTitle, Notice::getContent, Notice::getCreateTime)
                .eq(Notice::getState, "NORMAL")
                .orderByDesc(Notice::getId);
        if (CharSequenceUtil.isNotBlank(notice.getTitle())) queryWrapper.like(Notice::getTitle, notice.getTitle());
        if (CharSequenceUtil.isNotBlank(notice.getContent()))
            queryWrapper.like(Notice::getContent, notice.getContent());
        return Paging.build(noticeMapper.selectList(queryWrapper));
    }

    @PostMapping("/get")
    public Notice get(@RequestBody Notice notice) {
        return noticeMapper.selectOne(Wrappers.lambdaQuery(Notice.class)
                .select(Notice::getId, Notice::getTitle, Notice::getContent, Notice::getCreateTime)
                .eq(Notice::getId, notice.getId()));
    }

    @PostMapping("/get-for-show")
    public Notice getForShow(@RequestBody Notice notice) {
        final Notice notice1 = get(notice);
        if (CharSequenceUtil.isNotBlank(notice1.getContent())){
            notice1.setContent(notice1.getContent().replaceFirst("<p>(.*?)</p>", "$1").replaceAll("<img\\s+src\\s*=\\s*\"([^\"]+)[^>]*>", "<img src=\"$1\" style='max-width:100%;'>"));
        }
        return notice1;
    }

    @PostMapping("/delete")
    @RequiresAuthentication
    public void deleteExamDate(@RequestBody Notice notice) {
        noticeMapper.update(null, Wrappers.lambdaUpdate(Notice.class)
                .eq(Notice::getId, notice.getId())
                .set(Notice::getState, "DELETE"));
    }

    @PostMapping("/upsert")
    @RequiresAuthentication
    public void update(@RequestBody Notice notice) {
        if (notice.getId() == null) {
            notice.setId(Id.next());
            noticeMapper.insert(notice);
            return;
        }
        noticeMapper.update(null, Wrappers.lambdaUpdate(Notice.class)
                .eq(Notice::getId, notice.getId())
                .set(Notice::getTitle, notice.getTitle())
                .set(Notice::getContent, notice.getContent())
        );
    }
}
